<!DOCTYPE html>
<html lang="en" style="">
<head>
	<title>10-Autoload | PhalApi - A light-weight PHP framework for API</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="icon" href="http://webtools.qiniudn.com/dog_catch.png" type="image/x-icon" />
	<meta name="description" content="PhalApiis A light-weight PHP framework for API!">
	<meta name="keywords" content="PhalApi,phalapi,phalapi API develop,backend framework,restful,web services,saas,microservice,API framework,open API framework,PHP API framework,php web framework">
	<meta name="author" content="dogstar">

	<link rel="stylesheet" type="text/css" href="./../..//css/screen.css?20150211" />
</head>

<body>
<a href="https://github.com/phalapi/phalapi">
	<img alt="Fork me on GitHub" data-canonical-src="http://7xslqv.com1.z0.glb.clouddn.com/images/2/2d/9e69c0ebaff22a24e241a0244532e.png" src="http://7xslqv.com1.z0.glb.clouddn.com/images/2/2d/9e69c0ebaff22a24e241a0244532e.png" style="position: absolute; top: 0; right: 0; border: 0;">
</a>

<!-- 最顶部的语言(S) -->
<div class="grid-wrapper navbar desktop-only">
	<div class="grid">
		<div class="grid__cell">
			<ul id="language-switchers" class="navbar__links navbar--left">
                <li class="menu-item"><a title="PhalApi官方网站中文版" href="/" class="menu-item__link">Chinese</a></li>
				<li class="menu-item"><a title="English version of PhalApi website" href="/wikis/en/" class="menu-item__link">English</a></li>
			</ul>
			<ul id="util-menu" class="navbar__links navbar--right navbar--vertical-separator">
				<li class="menu-item"><a href="#" class="menu-item__link contact-us-spec">Welcome Here!</a></li>
			</ul>
		</div>
	</div>
</div>
<!-- 最顶部的语言(E) -->

<!-- 顶部导航菜单(S) -->
<div class="grid-wrapper desktop-only">
	<div class="grid">
		<div class="grid__cell">
			<div class="header__header-wrapper">
				<a title="PhalApi" href="http://www.phalapi.net" class="header__logo"><img src="http://webtools.qiniudn.com/master-LOGO-20150410_33.jpg" id="tw-logo" alt="PhalApi"></a>
				<ul id="main-menu" class="header__menu">
					<li class="menu-item"><a href="http://qa.phalapi.net/" class="menu-item__link insights-spec" target="_blank">Community</a></li>
					<li class="menu-item"><a href="https://github.com/phalapi/phalapi" class="menu-item__link insights-spec" target="_blank">Download</a></li>
					<li class="menu-item"><a href="/wikis/en/" class="menu-item__link events-spec" target="_blank">Wikis</a></li>
    				<li class="menu-item"><a href="/docs_en/" class="menu-item__link events-spec" target="_blank">Classes docs</a></li>
					<li class="menu-item"><a href="http://demo.phalapi.net/" class="menu-item__link products-spec" target="_blank">Demo</a></li>
					<li class="menu-item"><a href="/about.html" class="menu-item__link about-us-spec" target="_blank">About us</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
<!-- 顶部导航菜单(E) -->


    <div class="grid-wrapper">
        <div class="grid">
            <div class="grid__cell">
                <h2>10.1 The Principle of Autoload in PhalApi</h2>
<p>First of all, the autoload mechanism in PhalApi is very simple. Secondly, PhalApi can work very well with any other autoload mechanism, which means PhalApi do not limit you to only use one way to load file.  </p>
<p>In summery, the principle is: <strong>simple, uniform, normative</strong>. </p>
<h2>10.2 PEAR Package Naming Specification</h2>
<p>The relationship between class path and class name in PEAR package is very simple.
<img src="http://webtools.qiniudn.com/20150411005257_cec1c175fa70c8bc904389be30712bc3" alt="" />  </p>
<p>And it's much more simpler in PhalApi. We do not consider namespace yet, so let's ignore namespace. Assume we have these classes as below.  </p>
<pre><code>Api_User
Domain_User
Model_User</code></pre>
<p>We should have these related class files.  </p>
<pre><code>.
|-- Api
|   `-- User.php
|-- Domain
|   `-- User.php
|-- Model
|   `-- User.php</code></pre>
<p>Take another little more complicated class as an example. Class <code>Api_Game_User_Equitment</code> should be located at file <code>./Api/Game/User/Equitment.php</code>.  </p>
<blockquote>
<p>NOTE: Class name and file path are case-sensitive.  </p>
</blockquote>
<h2>10.3 Autoload By Mounting</h2>
<p>How could we make PhalApi autoload these classes that we just create?  </p>
<p>At default, PhalApi do not scan all the folders for performance reasons. It just scan framework and project root folder instead. When there are some new source ocde folders, we need to mount them into PhalApi. That means, we need to tell PhalApi where to find our new project members.  </p>
<p>We can finish that easily by using <a href="https://github.com/phalapi/phalapi/blob/release-en/PhalApi/PhalApi/Loader.php">PhalApi_Loader::addDirs()</a>.  </p>
<pre><code>DI()-&gt;loader-&gt;addDirs('Demo');</code></pre>
<p>We just add folder <code>Demo</code> into PhalApi, where our project codes are. If there are serveral folders, we can pass an array instead of a string.  </p>
<pre><code>DI()-&gt;loader-&gt;addDirs(array('Demo', 'Demo1', 'Demo2'));</code></pre>
<h3>(1) Add Relative Folder Paths</h3>
<p>Please keep in mind that we can only add relative folder paths that inside PhalApi. That is to say, any folders to be added should under path <code>API_ROOT</code>. On Linux, these three ways are equivalent.  </p>
<pre><code>// path: API_ROOT/Demo
DI()-&gt;loader-&gt;addDirs('Demo');

// path: API_ROOT/./Demo
DI()-&gt;loader-&gt;addDirs('./Demo');

// path: API_ROOT/Demo
DI()-&gt;loader-&gt;addDirs('/Demo');</code></pre>
<p>If the folder is not under folder PhalApi, we can solve this problem by using command <code>ls</code> on Linux.  </p>
<h3>(2) Add Single Absolute File Path</h3>
<p>We can add folder paths but also can add single file path by using <a href="https://github.com/phalapi/phalapi/blob/release-en/PhalApi/PhalApi/Loader.php">PhalApi_Loader::loadFile()</a>. It's different between passing relative file path and absolute file path when load single file.  </p>
<pre><code>// path: API_ROOT/Demo/Tool.php
DI()-&gt;loader-&gt;loadFile('Demo/Tool.php');

// path: /path/to/Demo/Tool.php
DI()-&gt;loader-&gt;loadFile('/path/to/Demo/Tool.php');</code></pre>
            </div>
        </div>
    </div>
    
 <!-- 广告位 -->
<!--
<div class="grid-wrapper desktop-only">
	<p align="center">
		<a href="http://7qnay5.com1.z0.glb.clouddn.com/走向开源的第一年%20-%20PhalApi%202015年度开源总结%20-%20官方出品%20-%2020151214.pdf" target="blank"><img width="950" height="100" src="http://7qnay5.com1.z0.glb.clouddn.com/PhalApi20151213-2.jpg"></a>
		<a href="http://qa.phalapi.net" target="blank"><img width="950" height="100" src="http://7qnay5.com1.z0.glb.clouddn.com/qa_ad_20150615.jpg"></a>
	</p>
</div>
-->

<!-- footer(S) -->
<div class="grid-wrapper footer">
	<div id="footer" class="grid">	

		<div class="grid__cell unit-1-2--lap">
			<h3><img src="http://webtools.qiniudn.com/master-LOGO-20150410_50.jpg" height="50"></h3>
            <p>
            <font size="3px;">PhalApi is a light-weight PHP framework for API.<br/>
                We try to keep PhalApi as stuip simple as possible!
            </font>
			</p>
			<p align="left">
				<font size="2px">&copy;2015-2016 PhalApi All Rights Reserved. 粤ICP备15028808号</font>
				<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1255326144'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1255326144%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
			</p>
		</div>	

		<div class="grid__cell unit-1-2--lap">
			<h3>Thanks!</h3>

			<div class="nav">
				<ul class="footer__nav">
					<li class="menu-item"><a href="http://www.oschina.net/" class="menu-item__link" target="_blank">Open Source China</a></li>
					<li class="menu-item"><a href="http://www.phalconphp.com/en/" class="menu-item__link" target="_blank">Phalcon</a></li>
					<li class="menu-item"><a href="https://phpunit.de/manual/3.7/zh_cn/automating-tests.html" class="menu-item__link" target="_blank">PHPUnit</a></li>
					<li class="menu-item"><a href="http://www.thoughtworks.com/cn/" class="menu-item__link" target="_blank">ThoughtWorks</a></li>
				</ul>
			</div>

            <div id="perspectives">
                <div class="email-signup">
                    <strong><a href="https://auth.alipay.com/login/index.htm" target="_blank">Alipay&nbsp;</a>Donate: </strong>chanzonghuang@gmail.com
                </div>
            </div>

			<p>
				<!-- JiaThis Button BEGIN -->
				<div id="ckepop">
				<span class="jiathis_txt">Share: </span>
				<a class="jiathis_button_tsina">Weibo</a>
				<a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">More</a>
				<a class="jiathis_counter_style"></a>
				</div>
				<script type="text/javascript" src="http://v2.jiathis.com/code/jia.js" charset="utf-8"></script>
				<!-- JiaThis Button END -->
				
			</p>
		</div>
	</div>
</div>
<!-- footer(E) -->

</body>

<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1254743218'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "w.cnzz.com/q_stat.php%3Fid%3D1254743218' type='text/javascript'%3E%3C/script%3E"));</script>

</html>
